package com.example.survey23.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.survey23.common.dto.QuestionDto;
import com.example.survey23.common.lang.QueryPageParam;
import com.example.survey23.common.lang.Result;
import com.example.survey23.entity.*;
import com.example.survey23.service.*;
import com.example.survey23.service.impl.SurveyBaseServiceImpl;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 23Group
 * @since 2022-11-01
 */
@RestController
@RequestMapping("/survey-base")
@Slf4j
public class SurveyBaseController {

    @Autowired
    SurveyBaseService surveyBaseService;

    @Autowired
    QuestionBankProblemService questionBankProblemService;

    @Autowired
    AnswerBankMultipleService answerBankMultipleService;

    @Autowired
    AnswerBankRadioService answerBankRadioService;

    @Autowired
    UserService userService;



    @PostMapping("/list")
    public List<SurveyBase> list(){
        return surveyBaseService.list();
    }

    // 新建问卷 —— 返回新建的问卷 id
    @PostMapping("/save")
    public  Result save(@RequestBody SurveyBase surveyBase){
        surveyBase.setId(surveyBaseService.getMaxSurveyBaseId() + 1);
        long userId = surveyBase.getUserId();
        User user = userService.getById(userId);
        user.setFee(user.getFee() + 0.5);
        userService.updateById(user);
        return surveyBaseService.save(surveyBase) ? Result.succ(surveyBase.getId()) : Result.fail(null);
    }


    @GetMapping("/update")
    public Result update(@RequestParam Integer id){
        SurveyBase surveyBase = surveyBaseService.getById(id);
        surveyBase.setState(1);
        return surveyBaseService.saveOrUpdate(surveyBase)? Result.succ(null) : Result.fail(null);
    }


    // 得到分页的数据结果
    @PostMapping("/listPageC1")
    public Result listPageC1(@RequestBody QueryPageParam queryPageParam) {

        // 查询分页数据 —— 传入当前页码和每页条数
        Page<SurveyBase> page = new Page<>();
        HashMap param = queryPageParam.getParam();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());
        LambdaQueryWrapper<SurveyBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(param != null){
            String name = (String) param.get("name");
            if(!"null".equals(name) && !StringUtils.isEmpty(name)) {
                lambdaQueryWrapper.eq(SurveyBase::getIsDelete, 0);
                lambdaQueryWrapper.like(SurveyBase::getSurveyName, (String) param.get("name"));
            }
        }
        lambdaQueryWrapper.eq(SurveyBase::getIsDelete, 0);
        IPage result = surveyBaseService.page(page, lambdaQueryWrapper);
        return Result.succ(result);
    }

    // 返回指定状态的问卷
    @PostMapping("/listPageStatus")
    public Result listPageStatus(@RequestBody QueryPageParam queryPageParam) { // 返回指定状态的问卷

        // 查询分页数据 —— 传入当前页码和每页条数
        Page<SurveyBase> page = new Page<>();
        HashMap param = queryPageParam.getParam();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        //List<SurveyBase> list =surveyBaseService.getSurveyByStatus(state);
        LambdaQueryWrapper<SurveyBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(param != null){
            String state = (String) param.get("bindValue"); // 得到对应的状态
            if(!"null".equals(state)) { // 筛选指定状态的问卷
                lambdaQueryWrapper.eq(SurveyBase::getState, state);
            }
        }
        IPage result = surveyBaseService.page(page, lambdaQueryWrapper);
        return Result.succ(result);
    }

    // 得到 id 为 id 的问卷的所有问题，封装为 QuestionDto 的 list 发回给前端
    @PostMapping("/preview")
    public Result listQuestionDto (@RequestBody HashMap map){
        HashMap<String, String> mapParam = (HashMap<String, String>) map.get("param");
        log.info("id为" + mapParam.get("id"));
        int id = Integer.parseInt(mapParam.get("id"));
        log.info("问卷 id 为"  + id);
        List<QuestionBankProblem> list = questionBankProblemService.getListByBelongId(id);
        // log.info(list.size() + "");
        List<QuestionDto> res = new ArrayList<>();

        // 初始化 res
        for(int i=0; i<list.size(); i++){
            QuestionDto questionDto = new QuestionDto();
            QuestionBankProblem questionBankProblem = list.get(i);
            int questionId = questionBankProblem.getId();
            String subject  = questionBankProblem.getQuName();
            int medolType = questionBankProblem.getQuType();
            questionDto.setMedolType(medolType);
            questionDto.setSubject(subject);
            List<String> options = new ArrayList<>();
            if(medolType == 1){ // 单选

                List<AnswerBankRadio> answerBankRadios = answerBankRadioService.getRadioOptions(questionId);
                for(AnswerBankRadio answerBankRadio : answerBankRadios){
                    options.add(answerBankRadio.getOptionName());
                }
            } else if(medolType == 2){ // 多选
                List<AnswerBankMultiple> answerBankMultiples = answerBankMultipleService.getMutipleOptions(questionId);
                for(AnswerBankMultiple answerBankMultiple : answerBankMultiples){
                    options.add(answerBankMultiple.getOptionName());
                }
            }
            questionDto.setOption(options);

            res.add(questionDto);
        }
        return Result.succ(res);
    }
    //彻底删除
    @GetMapping("/chedidelete")
    public Result chedidelete(@RequestParam String id){
        return surveyBaseService.removeById(id)? Result.succ(null) : Result.fail(null);
    }
    //逻辑删除
    @GetMapping("/delete")
    public Result delete(@RequestParam String id){
        SurveyBase surveyBase = surveyBaseService.findById(id);
        surveyBase.setIsDelete(1);
        return surveyBaseService.updateById(surveyBase)?Result.succ(null) : Result.fail(null);
    }
    //恢复
    @GetMapping("/redelete")
    public Result redelete(@RequestParam String id){
        SurveyBase surveyBase = surveyBaseService.findById(id);
        surveyBase.setIsDelete(0);
        return surveyBaseService.updateById(surveyBase)?Result.succ(null) : Result.fail(null);
    }
    @PostMapping("/listPageHS")
    public Result listPageHS(@RequestBody QueryPageParam queryPageParam) {

        // 查询分页数据 —— 传入当前页码和每页条数
        Page<SurveyBase> page = new Page<>();
        HashMap param = queryPageParam.getParam();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());
        LambdaQueryWrapper<SurveyBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(param != null){
            String name = (String) param.get("name");
            if(!"null".equals(name) && !StringUtils.isEmpty(name)) {
                lambdaQueryWrapper.eq(SurveyBase::getIsDelete, 1);
                lambdaQueryWrapper.like(SurveyBase::getSurveyName, (String) param.get("name"));
            }
        }
        lambdaQueryWrapper.eq(SurveyBase::getIsDelete, 1);
        IPage result = surveyBaseService.page(page, lambdaQueryWrapper);
        return Result.succ(result);
    }

}
